package com.example.schoolhelp.util;

import android.util.Log;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

/**
 * Created by Administrator on 2016/03/29.
 */
public class HttpUtils {
    static String sessionidRE = null;
    public static boolean isRegister = false;

    /**
     //     * post请求
     *
     * @param strUrl
     * @param connectTimeout
     * @param readTimeout
     * @param strJson
     * @return
     */
    public static String post(String strUrl, int connectTimeout, int readTimeout, String strJson) {
        StringBuffer result = new StringBuffer();
        JSONObject jsonObject = null;
        try {
            URL url = new URL(strUrl);
            byte[] datas = strJson.getBytes("UTF-8");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.setFixedLengthStreamingMode(datas.length);
            connection.setRequestProperty("Connection", "keep-alive");
            connection.setRequestProperty("Content-Length", String.valueOf(datas.length));
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(false);
            connection.setConnectTimeout(connectTimeout);
            connection.setReadTimeout(readTimeout);

            // 取得sessionid.
            if (isRegister) {
                if (sessionidRE != null) {
                    connection.setRequestProperty("cookie", sessionidRE);
                } else {
                    String cookieval = connection.getHeaderField("set-cookie");
                    if (cookieval != null) {
                        sessionidRE = cookieval.substring(0, cookieval.indexOf(";"));
                    }
                }
            }

            connection.connect();
            // 往服务器发送post的数据
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());

            out.write(datas);
            out.flush();
            out.close();

            // 获取服务器返回消息
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while (null != (line = bufferedReader.readLine())) {
                result.append(line);
            }
            Log.e("返回数据", result.toString());

            connection.disconnect();

            jsonObject = new JSONObject(result.toString());

        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

        return result.toString();
    }

    /**
     * get请求
     *
     * @param strUrl
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static String get(String strUrl, int connectTimeout, int readTimeout) {
        HttpURLConnection connection = null;
        StringBuffer result = new StringBuffer();
        JSONObject jsons = null;

        try {
            URL url = new URL(strUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setConnectTimeout(connectTimeout);
            connection.setReadTimeout(readTimeout);
            connection.connect();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while (null != (line = bufferedReader.readLine())) {

                result.append(line);
            }
            Log.e("返回数据2", result.toString());
            return result.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
        }
        return result.toString();
    }


}
